﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpotifyLync.Forms
{
    public partial class Settings : Form
    {

        private const string REMOTE_PC_HINT = "PC Name or IP Address";

        
        /// <summary>
        /// Creates a new instance of the settings form.
        /// </summary>
        public Settings()
        {
            InitializeComponent();
            InitializeRemotePCTextBox();
        }

        /// <summary>
        /// Handles the load event for the form.
        /// </summary>
        /// <param name="sender">The object invoking this event.</param>
        /// <param name="e">An object conatining any event arguments for this event.</param>
        private void Settings_Load(object sender, EventArgs e)
        {
            // Load the remote control settings.
            // NOTE: The other settings are bound in the designer.  The tertiary state logic of
            // radio buttons made binding rather dificult.  This was simple.
            if (Properties.Settings.Default.AllowExternalControl)
            {
                AllowRemoteControl.Checked = true;
            }
            else if (Properties.Settings.Default.DisableRemoting)
            {
                DisableRemote.Checked = true;
            }
            else if (!string.IsNullOrEmpty(Properties.Settings.Default.RemoteSpotifyPC))
            {
                RemotePC.Text = Properties.Settings.Default.RemoteSpotifyPC;
                RemotePC.ForeColor = Color.Black;
            }
            else
            {
                // We have no idea, so disable in this scenario.
                DisableRemote.Checked = true;
            }
            
        }


        #region Remote PC Textbox Voodoo

        /// <summary>
        /// Handles the check change event so as to unlock the remote pc textbox when appropriate.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemoteControlSpotify_CheckedChanged(object sender, EventArgs e)
        {
            RemotePC.ReadOnly = !RemoteControlSpotify.Checked;
        }

        /// <summary>
        /// Handles the behavior when a user clicks or tabs into the Remote PC textbox.
        /// </summary>
        /// <param name="sender">The object invoking this event.</param>
        /// <param name="e">An object conatining any event arguments for this event.</param>
        private void RemotePC_Enter(object sender, EventArgs e)
        {
            RemotePC.ForeColor = Color.Black;
            if (RemotePC.Text == REMOTE_PC_HINT)
            {
                RemotePC.Clear();
            }
        }

        /// <summary>
        /// Handles the behavior when a user clicks or tabs out of the Remote PC textbox.
        /// </summary>
        /// <param name="sender">The object invoking this event.</param>
        /// <param name="e">An object conatining any event arguments for this event.</param>
        private void RemotePC_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(RemotePC.Text))
            {
                InitializeRemotePCTextBox();
            }
        }

        /// <summary>
        /// Defaults the Remote PC TextBox to show a hint.
        /// </summary>
        private void InitializeRemotePCTextBox()
        {
            RemotePC.ForeColor = Color.Gray;
            RemotePC.Text = REMOTE_PC_HINT;
        }

        #endregion

        /// <summary>
        /// Cancels any changes, closes this form, and sends a cancel result to the calling entity.
        /// </summary>
        /// <param name="sender">The object invoking this event.</param>
        /// <param name="e">An object conatining any event arguments for this event.</param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Handles the click of the ok button, saving the data and closing the dialog.  This sends and OK result.
        /// </summary>
        /// <param name="sender">The object invoking this event.</param>
        /// <param name="e">An object conatining any event arguments for this event.</param>
        private void OKButton_Click(object sender, EventArgs e)
        {
            FinalizeSettings();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Saves the settings before closing the form.
        /// </summary>
        private void FinalizeSettings()
        {
            // NOTE:  Most of the settings are bound.  Only a few exceptions.
            if (AllowRemoteControl.Checked)
            {
                Properties.Settings.Default.AllowExternalControl = true;
            }
            else if (DisableRemote.Checked)
            {
                Properties.Settings.Default.DisableRemoting = true;
            }
            else if (!string.IsNullOrEmpty(RemotePC.Text))
            {
                Properties.Settings.Default.RemoteSpotifyPC = RemotePC.Text;
            }
            else
            {
                // We have no idea, so disable in this scenario.
                Properties.Settings.Default.DisableRemoting = true;
            }

            Properties.Settings.Default.Save();
        }

        private void EditBadWords_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProfanityFilter form = new ProfanityFilter();
            form.ShowDialog();
        }


    }
}
